package cn.oldsix.spark.core.tvplay

import org.apache.spark.Partitioner

/**
  * @ Author : Wu.D.J
  * @ Create : 2017.08.01
  * 自定义分区
  */
class TVPlayPartitioner(numPars : Int) extends Partitioner{
    
    override def numPartitions: Int = numPars
    
    override def getPartition(key: Any): Int = {
        var code = key.hashCode % numPartitions
        if (code < 0) {
            code + numPartitions
        } else {
            code
        }
    }
    
    override def hashCode(): Int = numPartitions
    
    override def equals(obj: scala.Any): Boolean =obj match {
        case tvplay : TVPlayPartitioner =>
            tvplay.numPartitions == numPartitions
        case _ =>
            false
    }
}
